package com.zkingsoft.zkingbase.widget;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.GridView;
import android.widget.ScrollView;

/**
 * Created by ytf on 2017/3/8 008.
 * Description: 上拉加载更多下拉刷新控件
 */

public class SwipeRefreshMoreLayout extends SwipeRefreshLayout
{
    private OnLoadingMoreListener loadingMoreListener;
    private View loadingMoreView;
    private AbsListView childMaybeAbsListView;
    private ScrollView childMaybeScrollView;
    private int height;
    private int width;
    private int loadingViewHeight;
    private int loadingViewWidth;

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        GridView gd;
        if(loadingMoreView != null)
        {
            loadingMoreView.measure(0,0);

            setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight() + loadingMoreView.getMeasuredHeight());
            loadingViewWidth = loadingMoreView.getMeasuredWidth();
            loadingViewHeight = loadingMoreView.getMeasuredHeight();
        }
        height = getMeasuredHeight();
        width = getMeasuredWidth();
        Log.e("s", "onMeasure: " + width + "--height="+height + "---loadingHeight="+loadingViewHeight + "---loadingWidth="+loadingViewWidth);
    }

    public void setOnLoadMoreListener(OnLoadingMoreListener l)
    {
        loadingMoreListener = l;
        loadingMoreView = l.getLoadingView();
        if(loadingMoreView == null)
            throw new RuntimeException("lodingview can't be null");
        addView(loadingMoreView);
        requestLayout();
    }

    public void setIsLoadingMore(boolean yn)
    {
        if(loadingMoreView != null)
        {
            if(yn)
            {
                loadingMoreView.setVisibility(View.VISIBLE);
            }else
            {
                loadingMoreView.setVisibility(View.GONE);
            }
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom)
    {
        super.onLayout(changed, left, top, right, bottom);

        if (getChildCount() > 0)
        {
            // 判断第一个孩子是不是ListView
            if (getChildAt(0) instanceof AbsListView)
            {
                childMaybeAbsListView = (AbsListView) getChildAt(0);
                // 设置滑动监听
                setOnScroll();
            }
        }
        if(loadingMoreView != null)
        {
            loadingMoreView.layout(left, getChildAt(0).getMeasuredHeight(), right, getChildAt(0).getMeasuredHeight()+loadingViewHeight);
        }
    }

    private void setOnScroll()
    {
        childMaybeAbsListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState)
            {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
            {
                // 移动过程中判断时候能下拉加载更多
            }
        });
    }

    public SwipeRefreshMoreLayout(Context context)
    {
        super(context);
    }

    public SwipeRefreshMoreLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }
}
